package com.easyShop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyShop.common.QueryPageParam;
import com.easyShop.common.Result;
import com.easyShop.entity.ProductCatagory;
import com.easyShop.service.ProductCatagoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2022-10-15
 */
@RestController
@RequestMapping("/productCatagory")
public class ProductCatagoryController {

    @Autowired
    private ProductCatagoryService productCatagoryService;;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody ProductCatagory productCatagory){
        return productCatagoryService.save(productCatagory)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody ProductCatagory productCatagory){
        return productCatagoryService.updateById(productCatagory)?Result.suc():Result.fail();
    }

    @PostMapping("/update/navStatus")
    public Result updateNavStatus(@RequestParam String id,@RequestParam Integer status){
        ProductCatagory productCatagory = new ProductCatagory();
        productCatagory.setId(Integer.valueOf(id));
        productCatagory.setNavStatus(status);
        return productCatagoryService.updateById(productCatagory)?Result.suc():Result.fail();
    }
    @PostMapping("/update/showStatus")
    public Result updateShowStatus(@RequestParam String id,@RequestParam Integer status){
        ProductCatagory productCatagory = new ProductCatagory();
        productCatagory.setId(Integer.valueOf(id));
        productCatagory.setShowStatus(status);
        return productCatagoryService.updateById(productCatagory)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return productCatagoryService.removeById(id)?Result.suc():Result.fail();
    }


    @PostMapping("/listPage/{parentId}")
    public Result listPage(@PathVariable Integer parentId, @RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<ProductCatagory> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<ProductCatagory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(ProductCatagory::getName, name);
        }
        // 确保 parentId 是有效的整数
        if (parentId != null) {
            lambdaQueryWrapper.eq(ProductCatagory::getParentId, parentId);
        }
        IPage<ProductCatagory> result = productCatagoryService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    @GetMapping("/list")
    public Result list(){
        List list = productCatagoryService.list();
        return Result.suc(list);
    }
}
